package cn.edu.swu.miniwebsvr.handler;

import cn.edu.swu.miniwebsvr.core.Handler;
import cn.edu.swu.miniwebsvr.core.Parameter;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class EchoHandler implements Handler {
    @Override
    public void handle(Parameter request, OutputStream response) {
        PrintWriter writer = new PrintWriter(response,true,StandardCharsets.UTF_8);
        Map<String, String> params = request.getAllParams();
        //构建响应体HTML内容
        StringBuilder contentBuilder = new StringBuilder();
        contentBuilder.append("<!DOCTYPE html>\r\n");
        contentBuilder.append("<html>\r\n");
        contentBuilder.append("<head>\r\n");
        contentBuilder.append("  <meta charset=\"UTF-8\">\r\n");
        contentBuilder.append("  <title>Echo Response</title>\r\n");
        contentBuilder.append("</head>\r\n");
        contentBuilder.append("<body>\r\n");

        // 回显请求方法和URL
        contentBuilder.append("<h1>Echo Server Response</h1>\r\n");
        contentBuilder.append("<h2>Request Details:</h2>\r\n");
        contentBuilder.append("<p><strong>Method:</strong> ").append(escapeHtml(request.getMethod())).append("</p>\r\n");
        contentBuilder.append("<p><strong>URL:</strong> ").append(escapeHtml(request.getUrl())).append("</p>\r\n");

        // TODO: [222024321072055/苏雨征] 根据接收到的参数Map构建HTML列表，回显给客户端
        contentBuilder.append("<h2>Parameters (").append(params.size()).append("):</h2>\r\n");
        if (params.isEmpty()) {
            contentBuilder.append("<p>No parameters received.</p>\r\n");
        } else {
            contentBuilder.append("<ul>\r\n");
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String key = escapeHtml(entry.getKey());
                String value = escapeHtml(entry.getValue());
                contentBuilder.append("<li><strong>").append(key).append
                        ("</strong>: ").append(value).append("</li>\r\n");
            }
            contentBuilder.append("</ul>\r\n");
        }
        contentBuilder.append("</body>\r\n");
        contentBuilder.append("</html>");
        String content = contentBuilder.toString();

        // TODO: [222024321072055/苏雨征] 构造完整的 HTTP 响应头
        writer.println("HTTP/1.1 200 OK");
        writer.println("Content-Type: text/html; charset=UTF-8");
        int contentLength=content.getBytes(StandardCharsets.UTF_8).length;
        writer.println("Content-Length: " + contentLength);
        writer.println("Connection: close");
        writer.println();
        //写入响应体并刷新
        writer.println(content);
        writer.flush();
    }

    //对HTML特殊字符进行转义，防止参数中包含HTML标签导致页面错乱
    private String escapeHtml(String input) {
        if (input == null) {
            return "";
        }
        return input.replace("&", "&amp;")
                .replace("<", "&lt;")
                .replace(">", "&gt;")
                .replace("\"", "&quot;")
                .replace("'", "&#39;");
    }
}

